// index.js
// 获取应用实例
const app = getApp()
const $req = require('../../utils/req.js') //接口封装文件

Page({
  data: {
    coupon: {
      mask: true,
      data: [],
    },
    tabbar: {}, //tarbar插件
    bannerList: [{
      url: '../../public/cache/banner.png'
    }], //轮播图
    selectArray: [],
    meetRooms: [],
    // ===================================
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    array: ['无视频', '有视频','全部'], //设备
    number:['20','50','100', '200','500'], //人数
    numIndex:20,
    index: 2,
    date:new Date().toISOString().substring(0, 10),
    time: '12:01',
    isphone:false,
    juanlist:[],
    infoss:null,
    datalogin:null,
    companyName:null
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  // ===========================================================================================
  /**
   *  通用跳转
   *  20200309 柴扬
   */
  toRoomDetail(e) {
    let _this = this
    let userInfo =  wx.getStorageSync('userInfo')
    if(!userInfo){
      wx.navigateTo({
        url: '../login/login',
      })
    }
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../orderDetail/orderDetail?id=' + id,
    })
  },
  /**
   * getCoupon 
   * 打开优惠券弹窗
   */
  getCoupon() {
    let _this = this
    _this.setData({
      'coupon.mask': true
    })
  },
  closeCouponMask(e) {
 
    let _this = this
    _this.setData({
      'coupon.mask': false
    })
  },

  // 接口方法

  // 获取首页的轮播图
  getBanner() {
    let _this = this
    $req.getBanner().then(res => {
      // let result = res.DATA
      res.DATA.forEach((item, index) => {
        item.Url  = item.Url.replace('http','https')
        console.log(item.Url)
      });
      _this.setData({
        bannerList: res.DATA
      })
    })
  },
  //获取首页列表数据
  getindexlist(index,numIndex,date){
    let that = this
    let data = {
      mdate: date,
      Peoples: numIndex,
      video: index
    }
    $req.getMeetRooms(data).then(res => {
      that.setData({
        meetRooms: res.DATA
      })
    })
  },
   /***选择器*/
   bindPickerChange: function (e) {
    let that = this
    let index = e.detail.value
    let numIndex = that.data.numIndex
    let date = that.data.date
    this.setData({
      index: e.detail.value
    })
    that.getindexlist(index,numIndex,date)
    // console.log('picker发送选择改变，携带值为', e.detail.value)
  },
  bindNumberChange(e){
    let that = this
    let index =that.data.index
    let numIndex = e.detail.value
    let date = that.data.date
    let number = that.data.number
    this.setData({
      numIndex: numIndex
    })
    that.getindexlist(index,number[numIndex],date)
    
  },
  bindDateChange: function (e) {
    let that = this
    let index =  that.data.index
    let numIndex = that.data.numIndex
    let date =e.detail.value
    this.setData({
      date:date
    })
    that.getindexlist(index,numIndex,date)
  },

  // 拨打电话
  ontel(e){
    let that = this
    let  token=  wx.getStorageSync('token')
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
        cmd: 'aboutme',
        token:token
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (data) {
        console.log(data.data.DATA)
        wx.makePhoneCall({
          phoneNumber: data.data.DATA.Mobile,
        })
      }
    });
  },
  getyhjlist(){
    let that = this
    //获取卷
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
        cmd: 'getListByMobile',
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (data) {
        if(data.data==''){
          that.setData({
            'coupon.mask': false
          })
        }
        if(data.data.STATUS=='Error'){
          that.setData({
            'coupon.mask': false
          })
        }else{
          let juanlist  = data.data.data.content
          if(juanlist.length>0){
          juanlist.forEach((item, index) => {
            item.giveEndTime =  that.time( item.giveEndTime)
            item.giveStartTime = that.time(item.giveStartTime)
          })
          that.setData({
          juanlist: juanlist
         })
        }else{
          that.setData({
            'coupon.mask': false
          })
        }
        }
      }
    });
  },
  //领取卷
  getaddyhj(e){
    let userInfo =  wx.getStorageSync('userInfo')
    let mobile = userInfo.mobile
    //点击事件id
    let id = e.currentTarget.dataset.id
    //判断是不是绑定了手机号
    if(mobile!=null){
      this.setData({
        isphone:true
      })
    }
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
        cmd: 'pickVoucher',
        mobile:mobile,
        voucherId:id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (data) {
        if(data.data.success){
          wx.showToast({
            title: '领取成功',
            icon: 'error',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: data.data.msg,
            icon: 'error',
            duration: 2000
          })
        }
      }
    });
  },

  //时间戳转化时间
  time(data) {
    let that=this
    let tomorrow = new Date(data);
    let year = tomorrow.getFullYear(); //获取年
    let month = tomorrow.getMonth() + 1; //获取月
    let date = tomorrow.getDate(); //获取日
    let tomorrowSS = year + '-' + month + "-" + date       
     return  tomorrowSS
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },

  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onShow(e){
 let  userInfo =  wx.getStorageSync('userInfo')
   //判断是不是绑定了手机号
   if(userInfo.mobile!=null){
    this.setData({
      isphone:false
    })
  }
  },
    /**
   *===========================================================================================
   * 生命周期函数--监听页面加载
   *===========================================================================================
   */
  onLoad(e){
    let that = this
    let index =  that.data.index
    let numIndex = that.data.numIndex
    let date =   that.data.date
    console.log(e.token)
     let wxinfo =  wx.getStorageSync('wxinfo');
      if(e.token!=undefined){
        let datalogin = {
          officeId:e.officeId,
          employeeId:e.employeeId,
          employeeName:e.employeeName,
          companyId:e.companyId,
          companyName:e.companyName,
          mobile:e.mobile,
          role:e.role,
          token:e.token
        }
        that.setData({
          datalogin:datalogin
        })
        wx.setStorageSync('datalogins',datalogin);
        if( wxinfo.nickName==undefined || wxinfo.nickName=='微信用户'){
          wx.navigateTo({
            url: '../login/login?datalogin=1',
          })
        }else{
            //直接开始登陆
              that.qiyegerlogin()
        }
        console.log('企业登陆')
      }else{
          //个人登陆
          if(wxinfo.nickName==undefined || wxinfo.nickName=='微信用户'){
            wx.navigateTo({
              url: '../login/login',
            })
          }else{
            that.userger()
          }
          console.log('个人登陆')
      }

    if (wx.getUserProfile) {
      that.setData({
        canIUseGetUserProfile: false
      })
    }
    wx.hideTabBar() //隐藏系统tabbar
    app.editTabbar(); // 全局方法：editTabbar()调用自定义组件底部导航栏
    that.getBanner();
    that.getindexlist(index,4,date)
    that.getyhjlist()
  },

  //个人登陆
  userger(){
    let that = this
    wx.login({
      success (res) {
        let code = res.code 
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
        code: code,
        cmd: 'wxlogin',
        officeid:1
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (data) {
        console.log(data)
        if(data.data.STATUS=='Success'){
          wx.setStorageSync('userInfo',data.data.DATA);
          wx.setStorageSync('token',data.data.DATA.token);
          that.setData({
            token:data.data.DATA.token,
            userInfo:data.data.DATA
          })
          if(data.data.DATA.companyName==''){
            that.eidtuserinfo()
          }
            // wx.switchTab({
            //   url: '../index/index',
            // })
        }
      }
    });
    }
  });
  },
  //企业登陆
  qiyegerlogin(){
    let that = this
    wx.login({
      success (res) {
        let code = res.code 
        let e =   that.data.datalogin 
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
        cmd: 'login',
        officeid:1,
        code: code,
        employeeId:e.employeeId,
        employeeName:e.employeeName,
        companyId:e.companyId,
        companyName:e.companyName,
        mobile:e.mobile,
        role:e.role,
        token:e.token,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (data) {
        console.log(data)
        if(data.data.STATUS=='Success'){
          wx.setStorageSync('userInfo',data.data.DATA);
          wx.setStorageSync('token',data.data.DATA.token);
          that.setData({
            token:data.data.DATA.token,
            userInfo:data.data.DATA
          })
            // wx.switchTab({
            //   url: '../index/index',
            // })
        }
      }
    });
      }
    })
  }
  

})